//
//  HNSH_AuthTool.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/28.
//

#import "HNSH_AuthTool.h"
#import <Photos/Photos.h>
#import <AVFoundation/AVFoundation.h>
#import <Intents/Intents.h>
#import <IntentsUI/IntentsUI.h>
#import <Contacts/Contacts.h>

@implementation HNSH_AuthTool



//判断用户是否获取相机权限
+ (void)requestCameragraph:(void(^)(BOOL isAuth))complete {
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusDenied
        || authStatus == AVAuthorizationStatusRestricted) {//用户已经明确否认
        NSString *messageStr = [NSString stringWithFormat:@"请打开设备的“设置”-“%@”-“相机”，并允许获得您的相机权限",[NSString appName]];
        UIAlertController *alterVC = [UIAlertController alertControllerWithTitle:@"请打开相机权限" message:messageStr preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [UIApplication.sharedApplication openURL:url options:@{} completionHandler:^(BOOL success) {
                
            }];
        }];
        [alterVC addAction:cancleAction];
        [alterVC addAction:confirmAction];
        [[UIViewControllerHelper findCurrentShowingViewController] presentViewController:alterVC animated:YES completion:nil];
        complete(NO);
    } else if (authStatus == AVAuthorizationStatusNotDetermined) {
        //第一次询问
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (granted) {
                    ///允许
                    complete(YES);
                } else {
                    complete(NO);
                }
            });
        }];
    } else {
        complete(YES);
    }
}

//判断用户是否获取相册权限
+ (void)requestForPhotoAuth:(void(^)(BOOL isAuth))complete {
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusNotDetermined) {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (status == PHAuthorizationStatusDenied
                    || status == PHAuthorizationStatusRestricted) {
                    [kSTWindow makeToast:@"未获得相册权限，暂无法导入图片"];
                    complete(NO);
                } else {
                    complete(YES);
                }
            });
        }];
    } else if (status == PHAuthorizationStatusRestricted ||
               status == PHAuthorizationStatusDenied) {
        //无权限
        NSString *messageStr = [NSString stringWithFormat:@"请打开设备的“设置”-“%@”-“相册”，并允许获得您的相册权限",[NSString appName]];
        UIAlertController *alterVC = [UIAlertController alertControllerWithTitle:@"请打开相册权限" message:messageStr preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [UIApplication.sharedApplication openURL:url options:@{} completionHandler:^(BOOL success) {
                
            }];
        }];
        [alterVC addAction:cancleAction];
        [alterVC addAction:confirmAction];
        [[UIViewControllerHelper findCurrentShowingViewController] presentViewController:alterVC animated:YES completion:nil];
        complete(NO);
    } else {
        complete(YES);
    }
}

+ (void)requestForAddressBookAuth:(void(^)(BOOL isAuth))block {
    //获取通讯录授权状态
    CNContactStore * contactStore = [[CNContactStore alloc]init];
    if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusNotDetermined) {
        //未知
        [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * __nullable error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (granted) {
                    //获得权限
                    block(YES);
                } else {
                    [kSTWindow makeToast:@"未获得通讯录权限，无法进行选择哦"];
                    block(NO);
                }
            });
        }];
    }
    else if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized){
        block(YES);
    }
    else {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            UIAlertController *alterController = [UIAlertController alertControllerWithTitle:@"提示" message:@"在您使用通讯录功能之前，需要您的同意才能进行哦" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            
            UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                [UIApplication.sharedApplication openURL:url options:@{} completionHandler:^(BOOL success) {
                    
                }];
            }];
            [alterController addAction:cancleAction];
            [alterController addAction:confirmAction];
            [[UIViewControllerHelper findCurrentShowingViewController] presentViewController:alterController animated:YES completion:nil];
        });
        block(NO);
    }
}

@end
